package com.shanzmoo.pay.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shanzmoo.pay.domain.request.storeRequest.SaobeiStoreAddRequest;
import com.shanzmoo.pay.domain.request.storeRequest.SaobeiStoreUpdateRequest;
import com.shanzmoo.pay.domain.response.storeResponse.SaobeiStoreAddResponse;
import com.shanzmoo.pay.domain.response.storeResponse.SaobeiStoreUpdateResponse;
import com.shanzmoo.pay.util.ApiConstants;
import com.shanzmoo.pay.util.ApiUtil;
import com.shanzmoo.pay.util.HttpClientUtils;
import com.shanzmoo.pay.util.RandomUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author dingmeng
 * @title: StoreManager
 * @projectName jyf-mall-server
 * @description: 扫呗--门店管理
 * @date 2021/4/415:28
 */
@Slf4j
@Component
public class SaobeiStoreManager {

    /**
     * 创建门店
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public SaobeiStoreAddResponse addStore(SaobeiStoreAddRequest request) {
        //发送请求
        log.info("创建门店请求报文:" + JSON.toJSONString(request));
        //参与签名的对象
        SaobeiStoreAddRequest signRequest = new SaobeiStoreAddRequest();
        signRequest.setInst_no(ApiConstants.INST_NO);
        signRequest.setMerchant_no(request.getMerchant_no());
        signRequest.setTrace_no(request.getTrace_no());
        signRequest.setStore_name(request.getStore_name());
        signRequest.setStore_phone(request.getStore_phone());
        signRequest.setStore_email(request.getStore_email());
        JSONObject obj = ApiUtil.convertMerchantRequest(request, signRequest, ApiConstants.key, "key");
        String xmlText = HttpClientUtils.post(ApiConstants.service_url_mch + "/store/100/add", obj.toJSONString());
        SaobeiStoreAddResponse response = JSON.parseObject(xmlText, SaobeiStoreAddResponse.class);
        log.info("创建门店返回报文:" + JSON.toJSONString(response));
        return response;

    }

    /**
     * 修改门店
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public SaobeiStoreUpdateResponse updateStore(SaobeiStoreUpdateRequest request) {
        //发送请求
        log.info("修改门店请求报文:" + JSON.toJSONString(request));
        //参与签名的对象
        SaobeiStoreUpdateRequest signRequest = new SaobeiStoreUpdateRequest();
        signRequest.setInst_no(ApiConstants.INST_NO);
        signRequest.setMerchant_no(request.getMerchant_no());
        signRequest.setTrace_no(request.getTrace_no());
        signRequest.setStore_code(request.getStore_code());
        JSONObject obj = ApiUtil.convertMerchantRequest(request, signRequest, ApiConstants.key, "key");
        String xmlText = HttpClientUtils.post(ApiConstants.service_url_mch + "/store/100/update", obj.toJSONString());
        SaobeiStoreUpdateResponse response = JSON.parseObject(xmlText, SaobeiStoreUpdateResponse.class);
        log.info("修改门店返回报文:" + JSON.toJSONString(response));
        return response;

    }

//    public static void main(String[] args) throws Exception {
//        //创建门店requst
//        SaobeiStoreAddRequest requst = new SaobeiStoreAddRequest();
//        requst.setInst_no(ApiConstants.INST_NO);
//        requst.setMerchant_no(ApiConstants.MERCHANT_NO);
//        requst.setStore_name("门店测试");
//        requst.setStore_addre("湖北省武汉市 硚口区 武汉市硚口区解放大道201号大武汉家居广场C地块(V座)13层3号");
//        requst.setStore_person("桂朝");
//        requst.setStore_phone("18516239965");
//        requst.setStore_email("291124965@qq.com");
//        requst.setTrace_no(RandomUtils.getRandom(32));
//        addStore(requst);
//    }
}
